package handel

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"logi-perm/cache"
	"logi-perm/model"
	"logi-perm/pkj"
	"strconv"
	"time"
)

func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "账号或密码错误",
		})
		return
	}
	redis_key := "login_error" + username
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if cacheData >= 3 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误三次，禁止登录",
		})
		return
	}
	var user model.User
	err := model.DB.Where("username = ?", username).Find(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "账号或密码错误",
		})
		return
	}
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "账号或密码错误",
		})
		return
	}
	if user.Password != pkj.Str(password) {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		if cacheData == 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "密码错误三次，封禁二小时",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"data": user,
		"msg":  "登陆成功",
	})
	return
}
func AddUserRole(c *gin.Context) {
	uid, _ := strconv.Atoi(c.PostForm("uid"))
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	data := model.UserRole{
		//Model: gorm.Model{},
		Uid: uid,
		Rid: rid,
	}
	model.DB.Debug().Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
}
func AddRoleNode(c *gin.Context) {
	nid, _ := strconv.Atoi(c.PostForm("nid"))
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	data := model.RoleNode{
		//Model: gorm.Model{},
		Rid: rid,
		Nid: nid,
	}
	model.DB.Debug().Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
}
func UserRoleList(c *gin.Context) {
	uid, _ := strconv.Atoi(c.Query("uid"))
	/*
		SELECT
		  *
		FROM
		  roles
		  LEFT JOIN user_roles ON roles.id = user_roles.rid
		WHERE
		  user_roles.uid = 1
	*/
	var roles model.Role
	model.DB.Table("roles").Joins("LEFT JOIN user_roles ON roles.id = user_roles.rid").
		Where("user_roles.uid = ?", uid).Debug().Find(&roles)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": roles,
	})
}
func RoleNodeList(c *gin.Context) {
	rid, _ := strconv.Atoi(c.Query("rid"))
	/*
			SELECT
		  *
		FROM
		  nodes
		  LEFT JOIN role_nodes ON nodes.id = role_nodes.nid
		WHERE
		  role_nodes.rid = 1
	*/
	var nodes model.Node
	model.DB.Table("nodes").Joins("LEFT JOIN role_nodes ON nodes.id = role_nodes.nid").
		Where("role_nodes.rid = ?", rid).Find(&nodes)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": nodes,
	})
}
func AdduserList(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size
	var Users []model.AddUser
	cache_key := "list_page_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, cache_key).Result()
	if cacheData == "" {
		model.DB.Offset(offset).Limit(size).Find(&Users)
		jsonData, _ := json.Marshal(&Users)
		cache.Rdb.Set(cache.Ctx, cache_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &Users)
	}

	var total int64
	model.DB.Model(&Users).Count(&total)

	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "ok",
		"data":  Users,
		"total": total,
	})

}
func AdduserDetail(c *gin.Context) {
	id := c.Query("id")
	var Users model.AddUser
	cache_key := "list_detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, cache_key).Result()
	if cacheData == "" {
		model.DB.Where("add_users.id=?", id).Find(&Users)
		jsonData, _ := json.Marshal(&Users)
		cache.Rdb.Set(cache.Ctx, cache_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &Users)
	}
	var total int64
	model.DB.Model(&Users).Count(&total)

	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "ok",
		"data":  Users,
		"total": total,
	})
}
